package com.boc.ewr.uniapp.api.controller;

import com.boc.ewr.clkIn.entity.CaDtdClkin;
import com.boc.ewr.clkIn.service.ICaDtdClkinService;
import com.boc.ewr.uniapp.api.vo.CaDtdClkinRespVo;
import com.boc.ewr.uniapp.api.vo.DtdClkInVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author : Wzz
 * @create : 2023-05- 24- 14:03
 * @desc :
 */
@Api(tags = "外出上门打卡Api")
@RestController
@RequestMapping("/uniapp/ca/dtd")
@Slf4j
public class CaDtdClkInApiController {

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private ICaDtdClkinService caDtdClkinService;

    @ApiOperation(value = "外出上门打卡-获取工号与姓名", notes = "外出上门打卡-获取工号与姓名")
    @GetMapping(value = "/getEmpNoNm")
    public Result<DtdClkInVo> getEmpNoNm() {
        DtdClkInVo dtdClkInVo = new DtdClkInVo();
        LoginUser loginUser = this.getLoginUser();
        String workNoByLoginUser = sysBaseAPI.getWorkNoByLoginUser(loginUser);
        dtdClkInVo.setEmpNo(workNoByLoginUser);
        dtdClkInVo.setRealname(loginUser.getRealname());
        return Result.OK(dtdClkInVo);
    }

    @ApiOperation(value = "外出上门打卡-打卡", notes = "外出上门打卡-打卡")
    @PostMapping(value = "/clickIn")
    public Result<String > clickIn(@RequestBody CaDtdClkinRespVo caDtdClkinRespVo) {
        CaDtdClkin caDtdClkin = new CaDtdClkin();
        BeanUtils.copyProperties(caDtdClkinRespVo,caDtdClkin);
        LoginUser loginUser = this.getLoginUser();

        String orgCode = loginUser.getOrgCode();
        caDtdClkin.setCaNo(orgCode);// 网点号
        String branchNo = sysBaseAPI.getParentDepartId(orgCode).getText();
        caDtdClkin.setBranchNo(branchNo);// 二级行号
        String iBranchNo = sysBaseAPI.getParentDepartId(branchNo).getText();
        caDtdClkin.setIbranchNo(iBranchNo);// 省行号
        caDtdClkin.setClkinTime(new Date());

        caDtdClkinService.save(caDtdClkin);
        return Result.OK("打卡成功！");
    }


    // 获取登录角色部门code
    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }
}
